package com.example.tranport.server;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.FilterEvent;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;


/****
 * 服务端的实现
 */
public class ServerTransPortImpl implements ServerTransPort {

    Logger logger = LoggerFactory.getLogger(getClass());

    private IoAcceptor acceptor;

    private ServerFactory serverFactory;

    @Override
    public void start() throws Exception {
        this.acceptor = new NioSocketAcceptor();
        this.acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        this.acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        this.acceptor.setHandler(new IoHandler() {
            @Override
            public void sessionCreated(IoSession ioSession) throws Exception {

            }

            @Override
            public void sessionOpened(IoSession ioSession) throws Exception {
                //客户端连接上来了.
            }

            @Override
            public void sessionClosed(IoSession ioSession) throws Exception {

            }

            @Override
            public void sessionIdle(IoSession ioSession, IdleStatus idleStatus) throws Exception {
                serverFactory.getServerMessageService().sessionIdle(ioSession, idleStatus);
            }

            @Override
            public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {

            }

            @Override
            public void messageReceived(IoSession ioSession, Object o) throws Exception {
                serverFactory.getServerMessageService().receive(ioSession, o);
            }

            @Override
            public void messageSent(IoSession ioSession, Object o) throws Exception {

            }

            @Override
            public void inputClosed(IoSession ioSession) throws Exception {
                serverFactory.getServerMessageService().closeSession(ioSession);
            }

            @Override
            public void event(IoSession ioSession, FilterEvent filterEvent) throws Exception {

            }
        });
        this.acceptor.bind(new InetSocketAddress(serverFactory.getPort()));
        logger.warn("Server started on port: {}", serverFactory.getPort());
    }

    @Override
    public void stop() throws Exception {
        this.acceptor.dispose();
    }

    public IoAcceptor getAcceptor() {
        return acceptor;
    }

    public void setAcceptor(IoAcceptor acceptor) {
        this.acceptor = acceptor;
    }

    public ServerTransPortImpl(ServerFactory serverFactory) {
        this.serverFactory = serverFactory;
    }
}
